package com.ruoyi.quartz.task;

import com.ruoyi.bus.domain.WitAppoint;
import com.ruoyi.bus.domain.WitPlan;
import com.ruoyi.bus.service.IWitAppointService;
import com.ruoyi.bus.service.IWitPlanService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description 任务计划
 * @Author caiyanshan
 * @Date 2021/5/11 10:01
 * @Param
 * @Return
 */
@Component("appointAndPlanStatusTask")
public class AppointAndPlanStatusTask {
  
  @Autowired
  private IWitPlanService planBusService;
  
  @Autowired
  private IWitAppointService appointService;
  
  @Autowired
  private ISysConfigService configService;
  
  /**
   * @Description
   * 1、将预约中前一天的所有已预约数据改为未发车，针对已上车的数据，需要通过车辆抵达目的地来更改
   * 2、将所有已排班的排班计划更改为未发车
   * @Author caiyanshan
   * @Date 2021/5/11 10:05
   * @Param []
   * @Return void
   */
  @Transactional
  public void updateStatus() {
    planBusService.updateWitPlanStatus();
    appointService.updateWitAppointStatus();
    
    //throw new RuntimeException();
  }

  private String getConfig(){
    return "0";
  }
  
  @Transactional
  public void overCar(){
    WitPlan plan = new WitPlan();
    plan.setPlanStatus(2);
    List<WitPlan> witPlanList = planBusService.selectWitPlanList(plan);
    if(witPlanList.size() == 0) {//没有行程中的车
      return;
    }
    for(WitPlan witPlan : witPlanList){
      WitPlan temp = witPlan;
      temp.setPlanStatus(1);
      planBusService.updateWitPlan(temp);
      String configFlag = this.getConfig();
      if(StringUtils.isBlank(configFlag)){
        configFlag = configService.selectConfigByKey("bus_over_driver_status_flag");
      }
      WitAppoint witAppoint = new WitAppoint();
      witAppoint.setPlanId(temp.getPlanId());
      if(StringUtils.isBlank(configFlag) || !StringUtils.equals("0", configFlag)){//用于测试
        appointService.updateOverAppoint(witAppoint);//分两种情况,1、已完成   2、未完成
        appointService.updateNotOverAppoint(witAppoint);
      }else{
        appointService.updateTestOverAppoint(witAppoint);//更新此次班车预约和已上车的为已完成
      }
    }
  }

}
